//
// C++ Implementation: server
//
// Description:
//
//
// Author: Thomas Richards <thomi@colon>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <QCoreApplication>
#include <QStringList>
#include <QProcess>
#include <QDebug>

#include "serverapp.h"
#include "results.h"
#include "serverconnection.h"

int main (int argc, char **argv)
{

	QCoreApplication app(argc, argv);

	// did the user ask for "-d"? If so, run in daemon mode:
	QStringList args = app.arguments();
	if (args.contains("-d"))
	{
		// remove -d, break infinite loop:
		args.removeAll("-d");

		// create process for background daemon, and start it up:
		qint64 pid;
		QString strAppName = args.takeFirst();
		if (QProcess::startDetached(strAppName, args, app.applicationDirPath(), &pid))
		{
			qDebug() << "Server daemon started. PID is:" << pid;
			return 0;
		}
		else
		{
			qDebug() << "Unable to start daemon";
			return 1;
		}
	}
	CServerApp serverApp;
	// listen by default:
	CServerConnection *sc = new CServerConnection(&app);
	sc->listen(16000);


	return app.exec();
}

